package alo.p6.work;

import org.junit.Test;
import alo.p6.BinaryTreeNode;
import alo.p6.Ergodic;

/**
 * @ClassName BuildTreeFromPreOrder
 * @Description 根据树的前序遍历构建树（规定该树结点只有0或2个孩子，I代表内部结点，L代表叶子结点）
 * @Author Fanxc
 * @DATE 2021/2/1 11:24
 * @Version 1.0
 */
public class BuildTreeFromPreOrder {
    BinaryTreeNode buildTreeFromPreOrder(char[] A, int i){
        if (A==null){
            return null;
        }
        BinaryTreeNode newNode=new BinaryTreeNode();
        newNode.setData(A[i]);
        newNode.setLeft(null);
        newNode.setRight(null);
        //若是遇到叶子结点，直接返回
        if (A[i]=='L'){
            return newNode;
        }
        i++;
        newNode.setLeft(buildTreeFromPreOrder(A,i));
        i++;
        newNode.setRight(buildTreeFromPreOrder(A,i));
        return newNode;
    }
    @Test
    public void test(){
        char[] A={'I','L','I','L','L'};
        BinaryTreeNode root = buildTreeFromPreOrder(A, 0);
        Ergodic ergodic = new Ergodic();
        ergodic.levelOrder(root);
        //73
        //76
        //73
        //76
        //76


    }
}
